package com.yvan.example;

import com.yvan.example.service.IdService;
import com.yvan.mvc.annotation.Pd;
import com.yvan.serverless.mvc.Model;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import java.util.LinkedHashMap;
import java.util.Map;

@RestController
@Slf4j
public class RootController {

    @Autowired
    private IdService idService;

    @GetMapping("/")
    public ModelAndView home() {
        return new ModelAndView(new RedirectView("/index.html"));
    }

    @ApiOperation("time")
    @GetMapping("/time")
    public Model<Map<String, Long>> time(@Pd(name = "t1", desc = "客户端时间") Long t1) {
        Map<String, Long> timeRp = new LinkedHashMap<>();
        timeRp.put("t1", t1);
        long start = System.currentTimeMillis();
        long dbTime = idService.getDbTime();
        long cost = System.currentTimeMillis() - start;
        timeRp.put("t2", dbTime);
        timeRp.put("t3", dbTime + cost);
        return Model.newSuccess(timeRp);
    }
}
